package tgc.edu.examxy.service;

import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import tgc.edu.examxy.custom.CommonService;
import tgc.edu.examxy.entity.Calendar;
import tgc.edu.examxy.repository.CalendarRepository;

@Service
public class CalendarService extends CommonService<Calendar,UUID>{
	@Autowired
	private CalendarRepository CalendarDAO;
	
	public List<Calendar> findByDatayear(String dfs) {
		Sort sort = Sort.by("sort");
		return CalendarDAO.findByDatayear(dfs,sort);
	}
	/***
	 * 自动生成当月的日历
	 */
	public void automatic() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
		String dfs = df.format(new Date());
		
		 LocalDate date = LocalDate.now();
	     int year = date.getYear();
	     int month = date.getMonthValue();
	     int day = date.getDayOfMonth();
	     // 将date重置为这个月的月初
	     date = date.minusDays(day-1);
	     DayOfWeek weekday = date.getDayOfWeek();
	     // 正式打印day
	     Integer sort = 1;
	     while (date.getMonthValue() == month){
	    	 
	    	 	Calendar ca = new Calendar();
	    	 	int monthone = date.getMonthValue();
	    	 	int dayone = date.getDayOfMonth();
	    	 	String monthtwo;
	    	 	String daytwo;
	    	 	if(monthone<10) {
	    	 		monthtwo = "0" + monthone;
	    	 	}else {
	    	 		monthtwo = "" +monthone;
	    	 	}
	    	 	if(dayone<10) {
	    	 		daytwo = "0" + dayone;
	    	 	}else {
	    	 		daytwo = "" +dayone;
	    	 	}
	            ca.setData(year+"-"+monthtwo+"-"+daytwo);
	            ca.setXq(""+date.getDayOfWeek().getValue());
	            ca.setDatayear(year+"-"+monthtwo);
	            ca.setDatar(""+dayone);
	            ca.setSort(sort);
	            save(ca);
	            // 当前天往后移动1天
	            sort++;
	            date = date.plusDays(1);
	        }
	}
	@Override
	public void save(Calendar entity) {
		// TODO Auto-generated method stub
		super.save(entity);
	}
	
}
